﻿using System;
using Common;
using Net;
using UI.Panels;
using UnityEngine;
using UnityEngine.UI;

namespace UI
{
    public class RegistryPanel : UIPanel
    {
        private Button m_BtnSignIn;
        private Button m_BtnSignUp;
        private InputField m_InputAccount;
        private InputField m_InputPhone;
        private InputField m_InputNickName;
        private InputField m_InputPassword;
        private InputField m_InputPasswordConfirm;

        private Text m_TextLog;
        
        protected override void Awake()
        {
            base.Awake();
            m_BtnSignIn = transform.Find("Btn_SignIn").GetComponent<Button>();
            m_BtnSignUp = transform.Find("Btn_SignUp").GetComponent<Button>();
            m_InputAccount = transform.Find("Input_Account").GetComponent<InputField>();
            m_InputPhone = transform.Find("Input_Phone").GetComponent<InputField>();
            m_InputNickName = transform.Find("Input_NickName").GetComponent<InputField>();
            m_InputPassword = transform.Find("Input_Password").GetComponent<InputField>();
            m_InputPasswordConfirm = transform.Find("Input_ConfirmPassword").GetComponent<InputField>();
            m_TextLog = transform.Find("Text_Log").GetComponent<Text>();
            
            m_BtnSignIn.onClick.AddListener(OnSignIn);
            m_BtnSignUp.onClick.AddListener(OnSignUp);
            
            transform.Find("Btn_Close").GetComponent<Button>().onClick.AddListener(Close);
        }

        protected override void OnEnable()
        {
            base.OnEnable();
            MessageDispatcher.RegisterMessageHandler<LS2U_UserLoginResponse>(LS2U_UserLoginResponse);
            EventCenter.AddListener<ConnectToServerEvent>(OnConnectedToServer);
        }

        private void OnConnectedToServer(ConnectToServerEvent e)
        {
            m_TextLog.text = "Connected to server";
        }

        private void OnSignIn()
        {
            Debug.Log("OnSignIn");
            var msg = new U2LS_UserLoginRequest();
            msg.account = m_InputAccount.text;
            msg.password = m_InputPassword.text;
            
            NetWorkManager.Instance.SendCmdMessageAsync(msg);
        }
        
        private void OnSignUp()
        {
            Debug.Log("OnSignUp");
            var msg = MessageFactory.Create<U2LS_UserRegisterRequest>() ;
            
            if (msg == null)
            {
                string info = "msg == null";
                m_TextLog.text = info;
                return;
            }
            
            msg.account = m_InputAccount.text;
            msg.phone = m_InputPhone.text;
            msg.nickname = m_InputNickName.text;
            msg.password = m_InputPassword.text;
            msg.confirm_pwd = m_InputPasswordConfirm.text;
            
            NetWorkManager.Instance.SendCmdMessageAsync(msg);
        }
        
        private void LS2U_UserLoginResponse(LS2U_UserLoginResponse msg)
        {
            if (msg == null)
            {
                string info = "msg == null";
                m_TextLog.text = info;
                return;
            }

            m_TextLog.text = msg.account;
        }

        private void Close()
        {
            UIManager.Instance.ClosePanel("RegistryPanel");
        }

        private void Update()
        {
            m_TextLog.text = Timer.Timer2String() ;
        }
    }
}